package xy001.imageloader_weichat;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.SQLOutput;
import java.util.List;

import xy001.imageloader_weichat.bean.FolderBean;

/**
 * 弹出popupWindow工具类
 * Created by xiangyang on 15/11/23.
 */
public class ListImageDirPopupWindow extends PopupWindow {

    private int mWith; //宽度
    private int mHeight;//高度
    private View mConvetView;
    private ListView mListView;
    private List<FolderBean> mDatas;

    public  OnDieSelectedListener mListener;

    /**
     * 回调接口
     */
    public interface  OnDieSelectedListener
    {
        void onSelected(FolderBean folderBean);
    }

    public void setOnDirSelectedListenner(OnDieSelectedListener mListener)
    {
        this.mListener=mListener;
    }
    public ListImageDirPopupWindow(Context context, List<FolderBean> datas) {
        calWitdAndHeight(context);
        mConvetView = LayoutInflater.from(context).inflate(R.layout.popup_main, null);
        mDatas = datas;

        setContentView(mConvetView);
        setWidth(mWith);
        setHeight(mHeight);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                    return true;
                }

                return false;
            }
        });

        initView(context);
        initEvent();
    }

    /**
     * 布局控件初始化
     * @param context
     */
    private void initView(Context context) {
        mListView = (ListView) mConvetView.findViewById(R.id.id_list_dir);
        mListView.setAdapter(new ListDirAdapter(context,mDatas));

    }

    /**
     * 事件初始化
     */
    private void initEvent() {

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener!=null)
                {
                    mListener.onSelected(mDatas.get(position));
                }
            }
        });
    }

    /**
     * 计算popupWindow的宽高
     */
    private void calWitdAndHeight(Context context) {
        WindowManager vm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        vm.getDefaultDisplay().getMetrics(outMetrics);

        mWith = outMetrics.widthPixels;
        mHeight = (int) (outMetrics.heightPixels * 0.7);


    }

    /**
     * popupWindow适配器
     */
    private class ListDirAdapter extends ArrayAdapter<FolderBean>

    {

        private LayoutInflater mInflater;
        private List<FolderBean> mDatas;


        public ListDirAdapter(Context context, List<FolderBean> objects) {
            super(context, 0, objects);
            mInflater = LayoutInflater.from(context);

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_popup, parent, false);
                holder.mImg = (ImageView) convertView.findViewById(R.id.id_dir_item_iamge);
                holder.mDirCount = (TextView) convertView.findViewById(R.id.id_dir_item_count);
                holder.mDirName = (TextView) convertView.findViewById(R.id.id_dir_item_name);
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            FolderBean bean = getItem(position);
            //重置代码
            holder.mImg.setImageResource(R.mipmap.pictures_no);
            //加载数据
            ImageLoader.getInstance(3, ImageLoader.Type.FIFI).loadImage(bean.getFirstImgPath(), holder.mImg);
            System.out.print(bean.getCount());
            holder.mDirCount.setText(bean.getCount()+"");
            holder.mDirName.setText(bean.getName());


            return convertView;
        }

        private class ViewHolder {
            ImageView mImg;
            TextView mDirName;
            TextView mDirCount;

        }
    }
}
